package util;



import com.mysql.cj.jdbc.Driver;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class ConnectionUtil {

    static String url;
    static String username;
    static String password;

    static {
        InputStream inputStream = ConnectionUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        url = properties.getProperty("url"); // = "jdbc:mysql://localhost:3306/clazz59?serverTimezone=GMT%2B8&characeterEncoding=utf-8";
        username = properties.getProperty("username"); // = "root";
        password = properties.getProperty("password"); // = "123456";
    }

    // 新建了一个连接
    public static Connection getConnection() throws Exception {
        //注册数据库驱动
        DriverManager.registerDriver(new Driver());
        // 把这些值放到配置文件中（获取数据库连接）
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }
}
